package com.zablert.lazyutil.drawable

import java.util.*

const val RETRACT = "    "
const val RETRACT2 = "        "

object ShapeUtil {
    private val shapeB: StringBuilder = StringBuilder()
    private val shapeMap: LinkedHashMap<String, String> = LinkedHashMap()
    fun head(): ShapeUtil {
        shapeMap.clear()
        shapeB.clear()
        shapeMap["head"] = """<?xml version="1.0" encoding="utf-8"?>"""
        shapeMap["shape"] = """<shape xmlns:android="http://schemas.android.com/apk/res/android">"""
        return this
    }

    fun addSolid(color: String): ShapeUtil {
        shapeMap["solid"] = "${RETRACT}<solid android:color=\"${color}\" />"
        return this
    }

    /**
     *  <stroke
    android:width="dimension"   //描边的宽度
    android:color="color"   //描边的颜色
    android:dashWidth="dimension"   //虚线的宽度，值为0时是实线
    android:dashGap="dimension" />
     */
    fun addStroke(width: Int, color: String, dashWidth: Int = 0, dashGap: Int = 0): ShapeUtil {
        shapeMap[" "] = "${RETRACT}<stroke\n" +
                "${RETRACT2}android:width=\"${width}dp\"\n" +
                "${RETRACT2}android:color=\"${color}\"\n" +
                if (dashGap != 0) {
                    "${RETRACT2}android:dashGap=\"${dashGap}dp\"\n"
                } else {
                    ""
                } +
                if (dashWidth != 0) {
                    "${RETRACT2}android:dashWidth=\"${dashWidth}dp\"\n"
                } else {
                    ""
                } +

                "${RETRACT2}/>"
        return this
    }

    fun addCornerRadius(radius: Int): ShapeUtil {
        shapeMap["radius"] = "${RETRACT}<corners android:radius=\"$radius\"/>"
        return this

    }

    fun addCornerRadius(lefTop: Int, rightTop: Int, rightBottom: Int, leftBottom: Int): ShapeUtil {
        shapeMap["radius"] = "${RETRACT}<corners\n" +
                "${RETRACT2}android:topLeftRadius=\"${lefTop}dp\"\n" +
                "${RETRACT2}android:topRightRadius=\"${rightTop}dp\"\n" +
                "${RETRACT2}android:bottomLeftRadius=\"${rightBottom}dp\"\n" +
                "${RETRACT2}android:bottomRightRadius=\"${leftBottom}dp\"\n" +
                "${RETRACT2}/>"
        return this
    }

    /**
     *  <gradient
     *  android:type=["linear" | "radial" | "sweep"]    //共有3中渐变类型，线性渐变（默认）/放射渐变/扫描式渐变
     *  android:angle="integer"     //渐变角度，必须为45的倍数，0为从左到右，90为从上到下（仅对线性渐变有效）
     *  android:centerX="float"     //渐变中心X的相当位置，范围为0～1(仅当渐变类型为放射渐变时有效)
     *  android:centerY="float"     //渐变中心Y的相当位置，范围为0～1(仅当渐变类型为放射渐变时有效)
     *  android:startColor="color"   //渐变开始点的颜色
     *  android:centerColor="color"  //渐变中间点的颜色，在开始与结束点之间
     *  android:endColor="color"    //渐变结束点的颜色
     *  android:gradientRadius="float"  //渐变的半径，只有当渐变类型为radial时才能使用
     *  android:useLevel=["true" | "false"] />  //使用LevelListDrawable时就要设置为true。设为false时才有渐变效果
     */
    @JvmOverloads
    fun addGradient(
        type: String = "linear",
        angle: Int = 0,
        centerX: Float = 0f,
        centerY: Float = 0f,
        startColor: String,
        centerColor: String? = null,
        endColor: String,
        gradientRadius: Float = 0f,
    ): ShapeUtil {
        shapeMap["gradient"] = "${RETRACT}<gradient\n" +
                "${RETRACT2}android:type=\"${type}\"\n" +
                "${RETRACT2}android:angle=\"${angle}\"\n" +
                "${RETRACT2}android:centerX=\"${centerX}\"\n" +
                "${RETRACT2}android:centerY=\"${centerY}\"\n" +
                "${RETRACT2}android:startColor=\"${startColor}\"" +
                if (!centerColor.isNullOrEmpty()) {
                    ""
                } else {
                    "\n${RETRACT2}android:centerColor=\"${startColor}\"\n"
                } +
                "${RETRACT2}android:endColor=\"${endColor}\"\n" +
                "${RETRACT2}android:gradientRadius=\"${gradientRadius}\"\n" +
                "${RETRACT2}/>"
        return this
    }

    fun toShape(): String {
        shapeMap["end"] = "</shape>"
        shapeMap.forEach {
            shapeB.appendLine(it.value)
        }
        return shapeB.toString()
    }

}